#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 13 22:43:40 2018

@author: XFBY
"""

import pandas as pd
from sklearn import metrics
import numpy as np
from tqdm import tqdm
import xgboost as xgb
from sklearn.externals import joblib
from sklearn.cross_validation import train_test_split
from xgboost import plot_importance
from sklearn.preprocessing import Imputer
from sklearn.linear_model import ARDRegression
from sklearn.linear_model import BayesianRidge
import matplotlib.pylab as plt
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler

all_columns = ['板温', '现场温度', '光照强度', 
               '转换效率', '转换效率A', '转换效率B', 
               '转换效率C', '电压A','电压B', '电压C', 
               '电流A', '电流B', '电流C', '功率A', 
               '功率B', '功率C', '平均功率', '风速',
               '风向']

col_redce =  ['板温', '光照强度','电压A',
               '电流A', '电流B', '电流C', '功率A', 
               '功率B', '功率C', '平均功率', '风速',
               '风向']

col_redce2 =  ['板温', '光照强度',
               '电流A', '电流B', '电流C', '功率A',
               '功率B', '功率C', '平均功率', '风速',
               '风向']


col = ['板温', '现场温度', '光照强度', '转换效率', '转换效率A', '转换效率B', '转换效率C', '电压A',
       '电压B', '电压C', '电流A', '电流B', '电流C', '功率A', '功率B', '功率C', '平均功率', '风速',
       '风向','t_diff', 'trans_diff', 'ps_avg', 'u_avg', 'u_pa', 'u_pb',
       'u_pc', 'a_avg', 'a_pa', 'a_pb', 'a_pc', 'p_avga', 'p_avgb', 'p_avgc']


coladd = ['板温', '现场温度', '光照强度', '转换效率', '转换效率A', '转换效率B', '转换效率C', '电压A',
       '电压B', '电压C', '电流A', '电流B', '电流C', '功率A', '功率B', '功率C', '平均功率', '风速',
       '风向','t_diff', 'tdiff_g', 'tdiff_x', 'trans_diff', 'ps_avg',
       'g_pa', 'g_pb', 'g_pc', 't_pa', 't_pb', 't_pc', 'u_avg', 'za', 'zb',
       'zc', 'z', 'all_u', 'all_a', 'u_pa', 'u_pb', 'u_pc', 'a_avg', 'a_pa',
       'a_pb', 'a_pc', 'p_avga', 'p_avgb', 'p_avgc']

data_train = pd.read_csv('public.train.csv')
data_test = pd.read_csv('public.test.csv')
submit_ex = pd.read_csv('submit_example.csv')

def piple_feature(data):
    '''
    no need converge
    add some columns
    
    '''
    data['t_diff'] = data['板温'] - data['现场温度']
    data['tdiff_g'] = data['t_diff']/data['光照强度']
    data['tdiff_x'] = data['t_diff']/data['现场温度']
    data['trans_diff'] = data['板温']/data['光照强度']
    data['ps_avg'] = (data['转换效率A']+data['转换效率B']+data['转换效率C'])/3
    data['g_pa'] = data['光照强度']/data['转换效率A']
    data['g_pb'] = data['光照强度']/data['转换效率B']
    data['g_pc'] = data['光照强度']/data['转换效率C']
    data['t_pa'] = data['板温']/data['转换效率A']
    data['t_pb'] = data['板温']/data['转换效率B']
    data['t_pc'] = data['板温']/data['转换效率C']
    data['u_avg'] = (data['电压A']+data['电压B']+data['电压C'])/3
    data['za'] = data['电压A']/data['电流A']
    data['zb'] = data['电压B']/data['电流B']
    data['zc'] = data['电压C']/data['电流C']
    data['z'] = data['za']+data['zb']+data['zc']
    data['all_u'] = data['电压A']+data['电压B']+data['电压C']
    data['all_a'] = data['电流A']+data['电流B']+data['电流C']
    data['u_pa'] = data['电压A']/(data['电压A']+data['电压B']+data['电压C'])
    data['u_pb'] = data['电压B']/(data['电压A']+data['电压B']+data['电压C'])
    data['u_pc'] = data['电压C']/(data['电压A']+data['电压B']+data['电压C'])
    data['a_avg'] = (data['电流A']+data['电流B']+data['电流C'])/3
    data['a_pa'] = data['电流A']/(data['电流A']+data['电流B']+data['电流C'])
    data['a_pb'] = data['电流B']/(data['电流A']+data['电流B']+data['电流C'])
    data['a_pc'] = data['电流C']/(data['电流A']+data['电流B']+data['电流C'])
    data['p_avga'] = data['功率A']/(data['功率A']+data['功率B']+data['功率C'])
    data['p_avgb'] = data['功率B']/(data['功率A']+data['功率B']+data['功率C'])
    data['p_avgc'] = data['功率C']/(data['功率A']+data['功率B']+data['功率C'])
    return data

if __name__ == '__main__':
#    feat_imp = pd.Series(rcf1.booster().get_fscore()).sort_values(ascending=False)
#    feat_imp.plot(kind='bar', title='Feature Importances')
#    plt.ylabel('Feature Importance Score')
#    plt.show()


    
    
    
    
    
    
    imputer = Imputer(missing_values='NaN', strategy='mean', axis=0)
    data = piple_feature(data_train)

#    imputer.fit(data[coladd])
   # xr = data[col];yr = data['发电量']
   # # xr = data_train[col_redce];yr = [float(x) for x  in data_train['发电量']]
    xr = data[coladd];yr = [float(x) for x  in data_train['发电量']]
    imputer.fit(xr)
    xr = imputer.transform(xr)
    xr = MinMaxScaler().fit_transform(xr)
    feature_train, feature_test, target_train, target_test = train_test_split(xr,yr,test_size=0.2)
    model = xgb.XGBRegressor(max_depth=5,learning_rate=0.15, n_estimators=600, silent=1)
    
    model.fit(feature_train, target_train)
    ans = model.predict(feature_test)
#    clf = RandomForestRegressor(n_estimators = 500,max_features = .8)
#    feature_train = imputer.transform(feature_train[coladd])
#    feature_test = imputer.transform(feature_test[coladd])
#    clf.fit(feature_train, target_train)
   #
    joblib.dump(model, 'xgb_add2.pkl')
#    joblib.dump(clf, 'rf_add2.pkl')
#   rcf1 = joblib.load('xgb_add.pkl')
#   rcf2 = joblib.load('xgb_no12.pkl')
#   rcf3 = joblib.load('xgb_no112.pkl')
#   rcf4 = joblib.load('xgb_no116.pkl')
   #
   #
#    ans2 = clf.predict(feature_test)
   #
    print("RMSE:",np.sqrt(metrics.mean_squared_error(target_test,ans)))
    print('the mean sqare error:%.2f' %np.mean(abs(ans-target_test)))
   #
#    print("RMSE:", np.sqrt(metrics.mean_squared_error(target_test, ans2)))
#    print('the mean sqare error:%.2f' % np.mean(abs(ans2 - target_test)))
   # test_data = piple_feature(data_test)
   # pre = rcf.predict(test_data[col])
#    result = pd.DataFrame(data_test['ID'])
#    result['Detection'] = [rcf.predict(data_test[data_test.ID==x][all_columns]) for x in list(result.ID)]
#    pre = pd.DataFrame({'Detection':rcf.predict(data_test[all_columns])})
#    pre = pd.DataFrame();pre['ID'] = data_test['ID']
#    pre = pd.DataFrame({'ID':data_test['ID']})
#    pre['Detection'] = model.predict(piple_feature(data_test)[coladd])
#   pre['Detection'] = (rcf1.predict(data_test[col_redce2])+
#								   rcf2.predict(data_test[col_redce])+
#												rcf3.predict(data_test[col_redce])+
#															 rcf4.predict(data_test[col_redce]))/4
#    
#    pre.to_csv('submitadd.csv',header=True,index=False)
    # pre.to_csv('result011.csv',header=False,index=False)
